﻿function goBack() {
    location.href = '/Library/Index';
}

function SubData() {
    var postData = {
        LibraryName: $("#Libraryname").val().trim(),
        Remarks: HtmlUtil.htmlEncodeByRegExp(CKEDITOR.instances.desTextarea.getData())
    };
    $.post('/Library/Addinfo', postData, function (result) {
        if (result == "suc") {
            goBack();
        }
        else if (result == "repeat") {
            $.alert({
                title: '系统提示',
                content: '重复数据无法添加！',
                confirm: function () {
                }
            });
        }
        else if (result == "nouser") {
            location.href = '/Login/Index';
        }
        else {
            $.alert({
                title: '系统提示',
                content: '操作失败！',
                confirm: function () {
                }
            });
        }
    });
}

$(document).ready(function () {

    CKEDITOR.replace('desTextarea', {
        toolbar: 'Basic'
        //uiColor: '#9AB8F3'//界面颜色
    });
    $("#regionselect").change(function () {
        var rid = $("#regionselect").val();
        if (rid == "") {
            $("#districtselect").empty();
        } else {
            $.ajax({
                data: { regionid: rid },
                type: "POST",
                url: "/Area/getdistrict",
                datatype: "json",
                success: function (data) {

                    var selectdata = data.date;
                    $("#districtselect").empty();
                    for (var i = 0; i < selectdata.length; i++) {
                        $("#districtselect").append('<option value=' + selectdata[i].DistrictID + '>' + selectdata[i].DistrictName + '</option>');
                    }
                }
            });
        }
    });

    $('#addareaForm')
       .bootstrapValidator({
           message: 'This value is not valid',
           feedbackIcons: {
               valid: 'glyphicon glyphicon-ok',
               invalid: 'glyphicon glyphicon-remove',
               validating: 'glyphicon glyphicon-refresh'
           },
           fields: {
               Libraryname: {
                   message: 'The Library Name is not valid',
                   validators: {
                       notEmpty: {
                           message: 'The Library Name is required and can\'t be empty'
                       }
                   }
               }

           }
       })
       .on('success.form.bv', function (e) {
           e.preventDefault();
           SubData();
       });
});